package com.briup.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.cms.dao.CategoryDao;
import com.briup.cms.dao.CommentDao;
import com.briup.cms.dao.UserDao;
import com.briup.cms.domain.Article;
import com.briup.cms.dao.ArticleDao;
import com.briup.cms.domain.Category;
import com.briup.cms.domain.Comment;
import com.briup.cms.domain.User;
import com.briup.cms.domain.param.ArticleQueryParam;
import com.briup.cms.domain.vo.ArticleVo;
import com.briup.cms.exception.ServiceException;
import com.briup.cms.service.IArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.cms.util.ResultCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author briup
 * @since 2023-03-09
 */
@Service
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    UserDao userDao;
    @Autowired
    CategoryDao categoryDao;
    @Autowired
    ArticleDao articleDao;
    @Autowired
    CommentDao commentDao;
    @Override
    public void saveOrUpdate(Article article) {
        if(article == null)
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        if(article.getId()==null){
            //新增操作
            if(article.getTitle()==null ||
               article.getContent()==null ||
               article.getUserId()==null||
                article.getCategoryId() == null)
                throw new ServiceException(ResultCode.PARAM_NOT_COMPLETE);
            //判断传入的userid和categoryid是否存在
            User user = userDao.selectById(article.getUserId());
            if(user == null)
                throw new ServiceException(ResultCode.DATA_NONE);
            Category category = categoryDao.selectById(article.getCategoryId());
            if(category == null)
                throw new ServiceException(ResultCode.DATA_NONE);
            article.setPublishTime(LocalDateTime.now());
            articleDao.insert(article);
        }else{
            //判断传入的文章是否存在
            Article article1 = articleDao.selectById(article.getId());
            if(article1 == null)
                throw new ServiceException(ResultCode.DATA_NONE);
            if(article1.getStatus().equals("审核通过"))
                throw new ServiceException(ResultCode.SPECIFIED_QUESTIONED_USER_NOT_EXIST);
            articleDao.updateById(article);
        }
    }

    @Override
    public void deleteById(Long id) {
        articleDao.deleteById(id);
    }

    @Override
    public void deleteByIdAll(List<Long> ids) {
        for (Long id : ids) {
            this.deleteById(id);
        }
    }

    @Override
    public IPage<Article> query(ArticleQueryParam param) {
        Page<Article> page = new Page<>(param.getPageNum(),param.getPageSize());
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(param.getCategoryId()!=null,Article::getCategoryId,param.getCategoryId());
        wrapper.eq(param.getStatus()!=null,Article::getStatus,param.getStatus());
        wrapper.like(param.getTitle()!=null,Article::getTitle,param.getTitle());
        wrapper.eq(param.getCharged()!=null,Article::getCharged,param.getCharged());
        wrapper.between(Article::getPublishTime,param.getStartTime(),param.getEndTime());
        articleDao.selectPage(page,wrapper);
        return page;
    }

    @Override
    public ArticleVo queryById(Long id) {
        Article article = articleDao.selectById(id);
        ArticleVo articleVo = new ArticleVo();
        BeanUtils.copyProperties(article,articleVo);
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Comment::getArticleId,id);
        wrapper.last("limit 3");
        List<Comment> comments = commentDao.selectList(wrapper);
        articleVo.setComments(comments);
        return articleVo;
    }
}
